<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryParams" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label-width="120px" label-position="left" label="项目名称" prop="crewName">
        <el-input v-model="queryParams.projectName" placeholder="采购项目名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!--<el-form-item label-width="120px" label-position="left" label="采购大类" prop="crewStatus">
        <el-select v-model="queryParams.largeCategoryId" placeholder="请选择采购大类" clearable>
          <el-option v-for="item in repairLargeCategoryOptions" :key="item.id" :label="item.projectCnName"
            :value="item.id" />
        </el-select>
      </el-form-item>-->
      <el-form-item label-width="120px" label-position="left">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--<el-col :span="12">
        <el-button type="primary" @click="openModal('大类维护')">大类维护</el-button>
        <el-button type="primary" @click="openModal('中类维护')">中类维护</el-button>
        <el-button type="primary" @click="openModal('小类维护')">小类维护</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:dept:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <el-col :span="12">
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-col>
    </el-row>
    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="resumeList"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="项目中文名称"  width="260" prop="projectCnName" />
      <el-table-column label="项目英文名称" align="center" prop="projectEnName" />
      <el-table-column label="显示排序" align="center" prop="projectSort" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            v-hasPermi="['system:resume:edit']"
            v-if="scope.row.projectParent != 0"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['system:resume:add']"
          >新增</el-button>
          <el-button
            v-if="scope.row.projectParent != 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:resume:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改部门对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24" v-if="form.parentId !== 0">
            <el-form-item label="上级项目" prop="projectParent">
              <treeselect v-model="form.projectParent" :options="deptOptions" :normalizer="normalizer" placeholder="选择上级项目" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目中文名称" prop="projectCnName">
              <el-input v-model="form.projectCnName" placeholder="请输入项目中文名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目英文名称" prop="projectEnName">
              <el-input v-model="form.projectEnName" placeholder="请输入项目英文名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="显示排序" prop="projectSort">
              <el-input-number v-model="form.projectSort" controls-position="right" :min="1" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPurchasingProject, getLargeCategoryList, getMediumCategoryList,
  addPurchasingProject, getPurchasingProject, editPurchasingProject, delPurchasingProject,
  uploads

} from "@/api/system/purchasingProject";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";



export default {
  name: "PurchasingProject",
  dicts: ['ships_type'],
  components: { Treeselect },
  data() {
    return {
      modalTitle: '',
      dictFlag: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 总条数
      total: 0,
      // 列表数据
      resumeList: [],
      // 树选项
      deptOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        largeCategoryId: null,
        projectName:'',
        ignoreId: null,
      },
      // 表单参数
      form: {
        projectParent: undefined,
        mediumCategoryId: undefined,
        projectCnIntroduction: undefined,
        projectEnIntroduction: undefined,
        projectCnName: undefined,
        projectEnName: undefined,
        projectSort:undefined,
        backgroundUrl:'',
      },
      // 表单校验
      rules: {
        projectCnName: [
          { required: true, message: "项目中文不能为空", trigger: "blur" }
        ],
        projectEnName: [
          { required: true, message: "项目英文不能为空", trigger: "blur" }
        ]
      },
      diaType: '',
      repairLargeCategoryOptions: [],//大类名称
      repairgetMediumCategoryOptions: [],//中类名称
    };
  },
  created() {
    this.getList();
    this.getLargeCategoryListFn()
  },
  methods: {
    setFileUrl(fileList){
      if(fileList){
        this.form.backgroundUrl=fileList
      }
    },
    beforeAvatarUpload(file) {
      console.warn(file,'---filea')
        const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
    // 获取大类
    getLargeCategoryListFn() {
      getLargeCategoryList().then((res) => {
        this.repairLargeCategoryOptions = res.data
      })
    },
    // 获取中类
    getMediumCategoryListFn() {
      getMediumCategoryList({ id: this.form.largeCategoryId }).then((res) => {
        this.repairgetMediumCategoryOptions = res.data
      })
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      if (row != undefined) {
        this.form.projectParent = row.id;
      }
      this.open = true;
      this.title = "添加项目";
      listPurchasingProject({}).then(response => {
        this.deptOptions = this.handleTree(response.data, "id","projectParent");
      });
    },
    /** 查询维修项目列表 */
    getList() {
      this.loading = true;
      listPurchasingProject(this.queryParams).then(response => {
        this.resumeList = this.handleTree(response.data, "id","projectParent");
        console.log(this.resumeList);
        this.loading = false;
      }).catch(() => {
        this.loading = false;
      });
    },
    /** 转换数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.projectCnName,
        children: node.children
      };
    },
    // 取消按钮
    cancel() {
      this.reset();
      this.dictFlag = false;

    },
    // 表单重置
    reset() {
      this.form = this.resetFormData(this.form);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = this.resetFormData(this.queryParams);
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleDetail(row) {
      this.reset();
      getPurchasingProject(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目";
        listPurchasingProject({ignoreId: row.id}).then(response => {
          this.deptOptions = this.handleTree(response.data, "id","projectParent");
          if (this.deptOptions.length == 0) {
            const noResultsOptions = { id: this.form.projectParent, projectCnName: this.form.projectCnName, children: [] };
            this.deptOptions.push(noResultsOptions);
          }
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id) {
            editPurchasingProject(this.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.dictFlag = false;
              this.queryParams.pageNum = 1;
              this.open = false;
              this.getList();
              this.getLargeCategoryListFn()
            });
          } else {
            addPurchasingProject(this.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.dictFlag = false;
              this.queryParams.pageNum = 1;
              this.open = false;
              this.getList();
              this.getLargeCategoryListFn()
            });
          }
        }
      });
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('此操作将删除该条数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delPurchasingProject(row.id).then((res) => {
          this.$modal.msgSuccess("操作成功");
          this.queryParams.pageNum = 1;
          this.getList();
        })
      }).catch(() => {

      })

    },
  }
};
</script>
<style>
.component-upload-image{
  display: flex;
  flex-wrap: wrap;
}
</style>
